package com.talos.ums.service.userstation;

import com.talos.framework.common.pojo.PageResult;
import com.talos.framework.common.util.object.BeanUtils;
import com.talos.ums.controller.admin.userstation.vo.UmsUserStationPageReqVO;
import com.talos.ums.controller.admin.userstation.vo.UmsUserStationSaveReqVO;
import com.talos.ums.dal.dataobject.userstation.UmsUserStationDO;
import com.talos.ums.dal.mysql.userstation.UmsUserStationMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import static com.talos.framework.common.exception.enums.GlobalErrorCodeConstants.DATA_NOT_FOUND;
import static com.talos.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用站点关联表 Service 实现类
 *
 * @author ${table.author}
 */
@Service
@Validated
public class UmsUserStationServiceImpl implements UmsUserStationService {

    @Resource
    private UmsUserStationMapper userStationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createUserStation(UmsUserStationSaveReqVO createReqVO) {
        // 插入
        UmsUserStationDO userStation = BeanUtils.toBean(createReqVO, UmsUserStationDO.class);
        userStationMapper.insert(userStation);
        // 返回
        return userStation.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserStation(UmsUserStationSaveReqVO updateReqVO) {
        // 校验存在
        validateUserStationExists(updateReqVO.getId());
        // 更新
        UmsUserStationDO updateObj = BeanUtils.toBean(updateReqVO, UmsUserStationDO.class);
        userStationMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserStation(Long id) {
        // 校验存在
        validateUserStationExists(id);
        // 删除
        userStationMapper.deleteById(id);
    }

    private void validateUserStationExists(Long id) {
        if (userStationMapper.selectById(id) == null) {
            throw exception(DATA_NOT_FOUND);
        }
    }

    @Override
    public UmsUserStationDO getUserStation(Long id) {
        return userStationMapper.selectById(id);
    }

    @Override
    public PageResult<UmsUserStationDO> getUserStationPage(UmsUserStationPageReqVO pageReqVO) {
        return userStationMapper.selectPage(pageReqVO);
    }

}
